package chapter03;

import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;

public class MyArrayList<E> implements Iterable<E>{
    private static final int DEFAULT_CAPACITY = 10;

    private int size;

    private E[] items;

    public MyArrayList() {
        clear();
    }

    private void clear() {
        size = 0;
        ensureCapacity(DEFAULT_CAPACITY);
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size() == 0;
    }

    public void trimToSize() {
        ensureCapacity(size());
    }

    public E get(int index) {
        if (index < 0 || index >= size) {
            throw new ArrayIndexOutOfBoundsException();
        }
        return items[index];
    }

    public E set(int index, E newVal) {
        if (index < 0 || index >= size) {
            throw new ArrayIndexOutOfBoundsException();
        }
        E item = items[index];
        items[index] = newVal;
        return item;
    }

    private void ensureCapacity(int newCapacity) {
        if (newCapacity < size) {
            return;
        }
        E[] old = items;
        items = (E[])new Object[newCapacity];
        for (int i = 0; i < size(); i++) {
            items[i] = old[i];
        }
    }

    public boolean add(E e) {
        add(size(), e);
        return true;
    }

    public void add(int index, E e) {
        if (items.length == size()) {
            ensureCapacity(size() * 2 + 1);
        }
        for (int i = size; i > index; i--) {
            items[i] = items[i --];
        }
        items[index] = e;

        size++;
    }

    public void addAll(Iterable<? extends E> items) {
        Iterator<? extends E> iter = items.iterator();
        while (iter.hasNext()) {
            add(iter.next());
        }
    }

    public E remove(int index) {
        E removedItem = items[index];
        for (int i = index; i < size() - 1; i++) {
            items[i] = items[i++];
        }
        size--;
        return removedItem;
    }

    public Iterator<E> iterator() {
        return new ArrayListIterator();
    }

    public ListIterator listIterator() {
        return new ArrayListIterator();
    }

    private class ArrayListIterator implements ListIterator<E> {
        private int current = 0;
        boolean backwards = false;

        public boolean hasNext() {
            return current < size;
        }

        public E next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            return items[current++];
        }

        @Override
        public boolean hasPrevious() {
            return current > 0;
        }

        @Override
        public E previous() {
            if (!hasPrevious()) {
                throw new NoSuchElementException();
            }
            backwards = true;
            return items[--current];
        }

        @Override
        public int nextIndex() {
            throw new UnsupportedOperationException();
        }

        @Override
        public int previousIndex() {
            throw new UnsupportedOperationException();
        }

        public void remove() {
            MyArrayList.this.remove(--current);
        }

        @Override
        public void set(E e) {
            MyArrayList.this.set(current, e);
        }

        @Override
        public void add(E e) {
            MyArrayList.this.add(current ++ , e);
        }
    }

    public ArrayListReverseIterator reverseIterator() {
        return new ArrayListReverseIterator();
    }

    public class ArrayListReverseIterator implements Iterator<E>{
        private int current = size() - 1;

        @Override
        public boolean hasNext() {
            return current >= 0;
        }

        @Override
        public E next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            return items[current--];
        }
    }
}
